
package com.healthcare;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

/**
 * Servlet implementation class UserRegistration
 */
public class SearchDoctor extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchDoctor() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String inputSpecialization = request.getParameter("specialization");

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aimsfirstconsultdb", "root",
					"");
			stmt = con.createStatement();

			String getDocSpecializationSql = "SELECT first_name,last_name,phone,hours,specialization,doctor_id from doctors where specialization='"
					+ inputSpecialization + "'";

			rs = stmt.executeQuery(getDocSpecializationSql);

			ArrayList<Doctor> docList = new ArrayList<>();
			// displaying records
			while (rs.next()) {
				String resfname = rs.getObject(1).toString();
				System.out.println(resfname);
				String reslname = rs.getObject(2).toString();
				System.out.println(reslname);
				String resphone = rs.getObject(3).toString();
				System.out.println(resphone);
				String reshours = rs.getObject(4).toString();
				System.out.println(reshours);
				String resspecialization = rs.getObject(5).toString();
				System.out.println(resspecialization);
				String resdocid = rs.getObject(6).toString();
				System.out.println(resdocid);

				Doctor doctor = new Doctor();
				doctor.setFirstname(resfname);
				doctor.setLastname(reslname);
				doctor.setPhone(resphone);
				doctor.setHours(reshours);
				doctor.setSpecialization(resspecialization);
				doctor.setDoctorid(resdocid);
				docList.add(doctor);
			}
			request.setAttribute("docList", docList);

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/DocSearchResult.jsp");
			dispatcher.forward(request, response);
		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (SQLException e) {
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String inputFilter = request.getParameter("query");

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aimsfirstconsultdb", "root",
					"");
			stmt = con.createStatement();

			if (inputFilter.matches("[0-9]+") && inputFilter.length() > 2) {
				String getDocSpecializationSql = "SELECT first_name,last_name,phone,hours,specialization,doctor_id from doctors "
						+ "where clinic_zip = " + inputFilter;

				rs = stmt.executeQuery(getDocSpecializationSql);
			} else {
				String getDocSpecializationSql = "SELECT first_name,last_name,phone,hours,specialization,doctor_id from doctors "
						+ "where first_name like '%"
						+ inputFilter
						+ "%' or last_name like '%" + inputFilter + "%'";

				rs = stmt.executeQuery(getDocSpecializationSql);
			}

			ArrayList<Doctor> docList = new ArrayList<>();
			// displaying records
			while (rs.next()) {
				String resfname = rs.getObject(1).toString();
				System.out.println(resfname);
				String reslname = rs.getObject(2).toString();
				System.out.println(reslname);
				String resphone = rs.getObject(3).toString();
				System.out.println(resphone);
				String reshours = rs.getObject(4).toString();
				System.out.println(reshours);
				String resspecialization = rs.getObject(5).toString();
				System.out.println(resspecialization);
				String resdocid = rs.getObject(6).toString();
				System.out.println(resdocid);

				Doctor doctor = new Doctor();
				doctor.setFirstname(resfname);
				doctor.setLastname(reslname);
				doctor.setPhone(resphone);
				doctor.setHours(reshours);
				doctor.setSpecialization(resspecialization);
				doctor.setDoctorid(resdocid);
				docList.add(doctor);
			}
			request.setAttribute("docList", docList);

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/DocSearchResult.jsp");
			dispatcher.forward(request, response);
		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (SQLException e) {
			}
		}
	}
}